package com.myfoon.utils;

import com.myfoon.model.entity.Location;
import com.myfoon.model.entity.User;
import com.myfoon.model.entity.result.EventOfFriend;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONUtils {
    public JSONUtils() {
    }

    public JSONObject convertFromEventOfFriends(List<EventOfFriend> list) {
        JSONObject jAllEvents = new JSONObject();
        JSONArray jArray = new JSONArray();

        int i = 0;

        try {
            for (EventOfFriend event : list) {
                JSONObject jEvent = new JSONObject();

                jEvent.put("event_id", event.getId());
                jEvent.put("event_message", event.getMessage());
                jEvent.put("event_photocount", event.getPhotoCount());
                jEvent.put("event_videocount", event.getVideoCount());
                jEvent.put("event_date", event.getDate());
                jEvent.put("event_created", event.getCreated());

                JSONObject jowner = new JSONObject();
                jowner.put("owner_id", event.getOwnerId());
                jowner.put("owner_fullname", event.getOwnerFullname());
                jowner.put("owner_photosmall", event.getOwnerPhotoSmall());

                JSONObject jlocation = new JSONObject();
                jlocation.put("location_id", event.getLocationId());
                jlocation.put("location_name", event.getLocationName());
                jlocation.put("location_city", event.getLocationCity());

                jEvent.put("owner", jowner);
                jEvent.put("location", jlocation);
                jArray.put(i++, jEvent);
            }
            jAllEvents.put("events", jArray);
        } catch (JSONException e) {
            ;
        }

        return jAllEvents;
    }

    public JSONObject convertFromLocations(List<Location> list) {
        JSONObject jAllLocations = new JSONObject();
        JSONArray jArray = new JSONArray();
        
        int i = 0;
        try {
            for (Location loc : list) {
                JSONObject jLoc = new JSONObject();

                jLoc.put("location_id", loc.getId());
                //jLoc.put("owner_id", loc.getOwner().getId());
                jLoc.put("location_name", loc.getName());
                jLoc.put("location_lat", loc.getLatitude());
                jLoc.put("location_lng", loc.getLongitude());
                jLoc.put("location_streetaddress", loc.getStreetaddress());
                jLoc.put("location_region", loc.getRegion());
                jLoc.put("location_city", loc.getCity());
                jLoc.put("location_country", loc.getCountry());
                
                jArray.put(i++, jLoc);
            }
            
            jAllLocations.put("locations", jArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jAllLocations;
    }
}
